<!-- 券包详情界面 -->
<template>
	<view class="coupon-details">
		<view class="content">
			<view class="top">
				<image :src="packetCouponData.cover"></image>
				<view class="top-content">
					<view>
						<text class="title">{{packetCouponData.name}}</text>
						<view class="amount">
							<text style="font-size: 28px;">￥{{packetCouponData.denomination}}</text>
						</view>
					</view>
					<view class="validity">券包数量：{{packetCouponData.couponSurfaceCount}}</view>
				</view>
			</view>
			<view class="tab-select">
				<view v-for="item in tabList" :key="item" class="tab-item" @click="tabChange(item)">
					<text :class="activateTab === item? 'activate-tab': 'default-tab'">{{item}}</text>
				</view>
			</view>
			<view v-if="activateTab === '券包'" class="packet">
				<view v-if="couponList.length > 0" class="packet-content">
					<view class="packet-item" v-for="(item1, index) in couponList" :key="index" :style="index>0 ? 'border-top: 1px solid #E7E7E7': ''">
						<image :src="item1.cover"></image>
						<view class="top-content">
							<view>
								<text class="title">{{item1.name}}</text>
								<view class="amount">
									<text style="font-size: 28px;">￥{{item1.denomination}}</text>
								</view>
							</view>
							<view class="validity">有效期：{{formatDate(item1.expirationTime)}}</view>
						</view>
					</view>
				</view>
			</view>
			<view v-if="activateTab === '介绍'" class="introduce">
				<uni-forms ref="baseForm" :modelValue="packetCouponData">
					<uni-forms-item label="介绍" >
						<text class="details">{{packetCouponData.description}}</text>
					</uni-forms-item>
				</uni-forms>
			</view>
			<view class="tip">
				最终解释权归望月湖银盆岭街道卫生服务中心所有。
			</view>
		</view>
		<view class="bottom">
			<button class="bottom-btn" type="default" @click="receive">立即领取</button>
		</view>
	</view>
</template>

<script setup lang="ts">
	import { onMounted, getCurrentInstance, ref } from 'vue';
	import dayjs from 'dayjs'
	import { getPacketCouponDetails, listPackageCouponSurfaces, receiveCouponPackage } from '@/sheep/api/mine.js'
	
	// 日期格式化
	const formatDate = (val) => {
		return dayjs(val).format('YYYY/MM/DD')
	}
	
	onMounted(()=>{
		couponId.value = getCurrentInstance().attrs.id
		loadPacketCouponDetails()
		loadListPackageCouponSurfaces()
	})
	
	// 券包id
	const couponId = ref()
	
	// 券包详情数据
	const packetCouponData = ref()
	
	// 获取券包详情
	const loadPacketCouponDetails = async() => {
		try{
			const res = await getPacketCouponDetails(couponId.value)
			packetCouponData.value = res.data
		}catch(e){
			console.log(e)
		}
	}
	const tabList = ref(['券包','介绍'])
	// 激活的tab
	const activateTab = ref('券包')
	const tabChange = (val) => {
		activateTab.value = val
	}
	
	// 券面列表
	const couponList = ref([])
	
	// 获取券包关联有效券面列表
	const loadListPackageCouponSurfaces = async() => {
		try{
			const res = await listPackageCouponSurfaces(couponId.value)
			couponList.value = res.data
		}catch(e){
			console.log(e)
		}
	}
	
	// 点击立即领取触发
	const receive = async() => {
		try{
			const data = {
				idNumber: '420103196504042096',
				couponSurfacePackageId: couponId.value
			}
			const res = await receiveCouponPackage(data)
			uni.showToast({
				icon: none,
				title: res.msg,
			});
		}catch(e){
			console.log(e)
		}
	}
</script>

<style lang="scss" scoped>
	.coupon-details {
		box-sizing: border-box;
		padding: 24px 0;
		height: 100%;
		background: linear-gradient(180deg, rgba(95, 206, 151, 1) 0%, rgba(255, 255, 255, 1) 100%);
		.content {
			height: calc(100vh - 110px);
			overflow: auto;
			.top {
				margin: 0 16px;
				display: flex;
				align-items: center;
				padding: 24px 16px;
				border-radius: 8px;
				background-color: #fff;
				image {
					height: 100px;
					width: 100px;
					border-radius: 8px;
				}
				.top-content {
					margin-left: 12px;
					display: flex;
					flex-direction: column;
					justify-content: space-between;
					height: 100px;
						.title {
							font-size: 16px;
							color: #333333;
							font-weight: bold;
						}
						.amount {
							display: flex;
							align-items: center;
							line-height: 40px;
							color: #FD5856;
							font-weight: 500;
						}
						.validity {
							font-size: 13px;
							color: #666666;
							font-weight: 400;
						}
				}
			}
			.tab-select {
				margin-top: 12px;
				background-color: #fff;
				display: flex;
				.tab-item {
					flex: 1;
					text-align: center;
					.activate-tab {
						padding: 8px;
						line-height: 40px;
						border-bottom: 2px solid #00907F;
					}
					.default-tab {
						padding: 8px;
						line-height: 40px;
					}
				}
			}
			.packet {
				margin: 12px 16px 0;
				.packet-content {
					border-radius: 8px;
					background-color: #fff;
					.packet-item {
						display: flex;
						align-items: center;
						padding: 24px 16px;
						image {
							height: 100px;
							width: 100px;
							border-radius: 8px;
						}
						.top-content {
							margin-left: 12px;
							display: flex;
							flex-direction: column;
							justify-content: space-between;
							height: 100px;
								.title {
									font-size: 16px;
									color: #333333;
									font-weight: bold;
								}
								.amount {
									display: flex;
									align-items: center;
									line-height: 40px;
									color: #FD5856;
									font-weight: 500;
								}
								.validity {
									font-size: 13px;
									color: #666666;
									font-weight: 400;
								}
						}
					}
				}
			}
			.introduce {
				margin: 12px 16px 0;
				padding: 16px 16px 0;
				border-radius: 8px;
				background-color: #fff;
				.details {
					color: #666;
					font-size: 16px;
				}
			}
			.tip {
				font-size: 14px;
				color: #9D9D9D;
				text-align: center;
				margin-top: 12px;
			}
		}
		.bottom {
			width: calc(100vw - 32px);
			position: fixed;
			bottom: 24px;
			left: 16px;
			.bottom-btn {
				background-color: #04B285;
				color: #fff;
				border-radius: 24px;
				font-size: 16px;
			}
		}
	}
	:deep(.uni-forms-item__label) {
		padding: 0;
		align-items: flex-start;
		height: auto;
		font-size: 16px;
	}
</style>